package com.sgb.kjwl.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.sgb.alimaplibrary.widget.MapMarkerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMapPosition3Binding;
import com.sgb.kjwl.model.entity.supplier3.SupplierEntity3;
import com.sgb.kjwl.model.entity.user.AddCarAddressEntity3;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

//地图定位
public class MapPositionActivity3 extends AppActivity {
    public static final int PICKUP_ADDRESS = 0;
    public static final int RETURN_ADDRESS = 1;
    public static final int MACHINE_ADDRESS = 2;
    public static final int DEVICE_ADDRESS = 3;
    private ActivityMapPosition3Binding binding;

    private Double currentLat;
    private Double currentLong;
    private List<SupplierEntity3> mapSupplierList;

    private String address = "";
    private AddCarAddressEntity3 addCarAddressEntity3;
    private int type = 0;
    private LatLng mFinalLatLng;
    private List<SuggestionCity> suggestionCityList = new ArrayList<>();
    private List<PoiItem> poiItems = new ArrayList<>();
    private int pos = 0;
    private SuggestionCity suggestionCity;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MapPositionActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.setClass(context, MapPositionActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude, int type) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.putExtra("type", type);
        intent.setClass(context, MapPositionActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerHomeComponent.builder()
//                .appComponent(appComponent)
//                .homeModule(new HomeModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(MapPositionActivity3.this, R.layout.activity_map_position3);
        binding.include.backView.setVisibility(View.VISIBLE);
        binding.include.backView.setOnClickListener(this);
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setText("完成");
        binding.include.rightView.setOnClickListener(this);
        binding.include.titleView.setText("获取详细地址");
    }

    @Override
    protected void init() {
        currentLong = getIntent().getDoubleExtra("longitude", 0);
        currentLat = getIntent().getDoubleExtra("latitude", 0);
        type = getIntent().getIntExtra("type", 0);
        addCarAddressEntity3 = new AddCarAddressEntity3();
        addCarAddressEntity3.setLongitude(currentLong+"");
        addCarAddressEntity3.setLatitude(currentLat+"");

    }

    private void initView(Bundle savedInstanceState) {
        initMap(savedInstanceState);
        initPoiSearch();
    }

    private void initMap(Bundle savedInstanceState) {
        binding.mMapView.initMap(savedInstanceState, getLifecycle(), new MapMarkerView.CallBack() {
            @Override
            public void onMarkerClick(int pos, String title) {
                showMarkerView(pos,title);
            }

            @Override
            public void onLocation(Double Lat, Double Log, String string) {
                if (mFinalLatLng.longitude != 0 && mFinalLatLng.latitude !=0) return;
                mFinalLatLng = new LatLng(Lat, Log);
                addCarAddressEntity3.setLongitude(Log+"");
                addCarAddressEntity3.setLatitude(Lat+"");
            }

            @Override
            public void getAddress(Double Lat, Double Log,String address) {
                super.getAddress(Lat,Log,address);
                mFinalLatLng = new LatLng(Lat, Log);
                addCarAddressEntity3.setLongitude(Log+"");
                addCarAddressEntity3.setLatitude(Lat+"");
            }

            @Override
            public void onRegeocodeSearched(Double Lat, Double Log, String string) {
                super.onRegeocodeSearched(Lat,Log,string);
                //详细地址
                address = string;
                addCarAddressEntity3.setAddressName(address);
            }
        });
        binding.mMapView.setMapClick();
    }

    private void initPoiSearch() {
        if (currentLat !=0 && currentLong != 0){
            LatLng latLng = new LatLng(currentLat, currentLong);
            binding.mMapView.setMyLocation(currentLat,currentLong,R.mipmap.map_goods_mark);
            binding.mMapView.getLatLotExtra(currentLat,currentLong);
        }
    }

    public void showMarkerView(int pos,String title) {
        PoiItem poiItem = binding.mMapView.getPoiItems().get(pos);
        mFinalLatLng = new LatLng(poiItem.getLatLonPoint().getLatitude(), poiItem.getLatLonPoint().getLongitude());
        binding.mMapView.setMarkerIcon(pos,false);
        address = poiItem.getProvinceName() + poiItem.getCityName() + poiItem.getAdName() + poiItem.getSnippet();
        addCarAddressEntity3.setLongitude(mFinalLatLng.longitude + "");
        addCarAddressEntity3.setLatitude(mFinalLatLng.latitude+"");
        addCarAddressEntity3.setAddressName(address);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.right_view:
                if (type == PICKUP_ADDRESS) {
                    EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_MAP_POSITION_CAR).setMessageData(addCarAddressEntity3));
                } else if (type == RETURN_ADDRESS) {
                    EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_MAP_POSITION_CAR_REUTRN).setMessageData(addCarAddressEntity3));
                } else if (type == MACHINE_ADDRESS) {
                    EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_MAP_POSITION_MACHINE_REUTRN).setMessageData(addCarAddressEntity3));
                } else if (type == DEVICE_ADDRESS) {
                    EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_MAP_POSITION_DEVICE_REUTRN).setMessageData(addCarAddressEntity3));
                }
                finish();
                break;
        }
    }
}
